/*
 *
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see http://www.gnu.org/licenses/. 
 *
 *  Authors:
 *  	Albert Marieges Garcia
 *  	Álex Pardo Fernández
 *  	Enric Mayas Márquez
 *  	Juan Carlos Calvo Tejedor
 *  	Marc Gironés Dezsényi
 *  
 *  Contact:
 *  	bcn.tours.android@gmail.com
 *  Webpage:
 *  	bcntours.co.cc
 *
 */
package com.BCNTours.AR.Vista;


import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.view.View;

import com.BCNTours.R;
import com.BCNTours.AR.Utils.Coordinates;

/**
 * This class draws the arrow which markes the direction on the AR route view.
 *
 */
public class DrawArrow extends View { 
	
	private float width;
	private float height;
	private Coordinates destination;
	private float angle = 0;
	
	@SuppressWarnings("unused")
	private Paint paint = new Paint();
	@SuppressWarnings("unused")
	private float mTextBaseline; 
	
	/**
	 * Creates a DrawArrow object
	 */
    public DrawArrow(Context context) { 
            super(context); 
            // TODO Auto-generated constructor stub
    } 
    public DrawArrow(Context context, Coordinates dest) { 
        super(context); 
        destination = dest;
        // TODO Auto-generated constructor stub
} 
    @Override 
    protected void onDraw(Canvas canvas) { 
    	
            // TODO Auto-generated method stub 
    		width = canvas.getWidth();
    		height = canvas.getHeight();
            
            Bitmap eBitmap = BitmapFactory.decodeResource(getResources(), R.drawable.arrow);
            
            int w = eBitmap.getWidth();
            int h = eBitmap.getHeight();
            // Setting post rotate to 90
            Matrix mtx = new Matrix();
            mtx.setRotate(angle,eBitmap.getWidth()/2,eBitmap.getHeight()/2);
            // Rotating Bitmap
            Bitmap rotatedBMP = Bitmap.createBitmap(eBitmap, 0, 0, w, h, mtx, true);
            
            canvas.drawBitmap(rotatedBMP ,(width/2)/(w/2),(height/2)-(h/2),  null) ;

            
            super.onDraw(canvas); 
    } 
    
    @Override
	protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
    	// TODO Auto-generated method stub
    	setMeasuredDimension(MeasureSpec.getSize(widthMeasureSpec), MeasureSpec.getSize(heightMeasureSpec));
    }
    
    /**
     * Sets the arrow new state.
     * @param newLocation Coordinates of the new location
     * @param azimuth Angle of the new location
     */
    public void setCurrentPosition(Coordinates newLocation, float azimuth){
    	angle = newLocation.getAngleToCam(destination, azimuth + 90);
    	invalidate();
    }
   
   /**
    * Sets the new destination.
    * @param dest Destination
    */
   public void setDest(Coordinates dest){
	   destination = dest;
	   invalidate();
   }
} 